import EventEmitter from 'eventemitter3'

export type IDropEventTypeKey = 'enter' | 'leave' | 'drop'

class DropEventCenter {
    /** 事件触发器 */
    eventEmitter = new EventEmitter<`${string}_${IDropEventTypeKey}`>()

    constructor() {}

    getEventName(droppableId: string, type: IDropEventTypeKey): `${string}_${IDropEventTypeKey}` {
        return `${droppableId}_${type}`
    }

    /** 注册事件 */
    emit(droppableId: string | undefined, type: IDropEventTypeKey, ...args: any[]) {
        if (!droppableId) return
        this.eventEmitter.emit(this.getEventName(droppableId, type), ...args)
    }

    /** 监听事件 */
    on(droppableId: string | undefined, type: IDropEventTypeKey, listener?: (...args: any[]) => void) {
        if (!droppableId || !listener) return
        this.eventEmitter.on(this.getEventName(droppableId, type), listener)
    }

    /** 移除监听 */
    off(droppableId: string | undefined, type: IDropEventTypeKey, listener?: (...args: any[]) => void) {
        if (!droppableId || !listener) return
        this.eventEmitter.off(this.getEventName(droppableId, type), listener)
    }
}

export const dropEventCenter = new DropEventCenter()
